import moment from 'moment'

let that = new Utils();

function Utils() {
    this.moment = moment;
};

/**
 * 时间戳
 * @param {*} timestamp  时间戳
 */
Utils.prototype.timestampToTime = (timestamp) => {
    let date = new Date(timestamp) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    let Y = date.getFullYear() + '-'
    let M =
        (date.getMonth() + 1 < 10 ?
            '0' + (date.getMonth() + 1) :
            date.getMonth() + 1) + '-'
    let D =
        (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
    let h =
        (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    let m =
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
        ':'
    let s =
        date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    return Y + M + D + h + m + s
};
/**
 * 存储localStorage
 */
Utils.prototype.setStore = (name, content) => {
    if (!name) return;
    if (typeof content !== 'string') {
        content = JSON.stringify(content);
    }
    window.localStorage.setItem(name, content);
}

/**
 * 获取localStorage
 */
Utils.prototype.getStore = name => {
    if (!name) return;
    return window.localStorage.getItem(name);
}

/**
 * 删除localStorage
 */
Utils.prototype.removeStore = name => {
    if (!name) return;
    window.localStorage.removeItem(name);
}

/**
 * 设置cookie
 **/
Utils.prototype.setCookie = (name, value, day) => {
    let date = new Date();
    date.setDate(date.getDate() + day);
    document.cookie = name + '=' + value + ';expires=' + date;
};

/**
 * 获取cookie
 **/
Utils.prototype.getCookie = (name) => {
    let reg = RegExp(name + '=([^;]+)');
    let arr = document.cookie.match(reg);
    if (arr) {
        return arr[1];
    } else {
        return '';
    }
};

/**
 * 删除cookie
 **/
Utils.prototype.delCookie = (name) => {
    utils.setCookie(name, null, -1);
};

/**
 * 转换
 * k = 键
 * v = 值
 * o = 类型对象
 * l = 字典
 * 类型,1、array数组  2、date时间  3、datetime具体时间
 */

Utils.prototype.formatterForLable = (k, v, o, l) => {
    if (v == null || v == "") {
        return "--"
    }

    if (o[k] == null) {
        return v
    }

    let t = o[k];
    if (t == "array") {
        for (let i = 0; i < l.length; i++) {
            if (l[i]["value"] == v) {
                return l[i]["label"];
            }
        }
    } else if (t == "date") {
        return that.moment(v).format('YYYY-MM-DD')
    } else if (t == "datetime") {
        return that.moment(v).format('YYYY-MM-DD HH:mm:ss')
    }
}

/**
 * 转换
 * r = 数据对象
 * o = 类型对象
 * d = 字典
 * 类型,1、array数组  2、date时间  3、datetime具体时间
 */

Utils.prototype.formatterForForm = (r, o, d) => {
    if (r == null || r == "") {
        return
    }

    let result = {};

    for (let k in r) {
        let v = r[k];
        let t = o[k] != null && o[k] != '' ? o[k] : '';
        if (t == "array") {
            for (let i = 0; i < d[k].length; i++) {
                if (d[k][i]["value"] == v) {
                    result[k] = d[k][i]["label"];
                }
            }
        } else if (t == "date") {
            result[k] = that.moment(v).format('YYYY-MM-DD');
        } else if (t == "datetime") {
            result[k] = that.moment(v).format('YYYY-MM-DD HH:mm:ss');
        } else {
            result[k] = v;
        }
    }

    return result
}

/**
 * 计算文件大小
 * @param s
 */
Utils.prototype.getFileSize = (s) => {
    if (!s)
        return 0

    let n = 1024.00; //byte

    if (s < n)
        return s + "B";
    if (s < Math.pow(n, 2))
        return (s / n).toFixed(2) + "K"; //kb
    if (s < Math.pow(n, 3))
        return (s / Math.pow(n, 2)).toFixed(2) + "M"; //M
    if (s < Math.pow(n, 4))
        return (s / Math.pow(n, 3)).toFixed(2) + "G"; //G
    return (s / Math.pow(n, 4)).toFixed(2) + "T"; //T
}

Utils.prototype.randomString = (e) => {
    e = e || 32;
    let t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
        a = t.length,
        n = "";
    for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
    return n
}

export const utils = new Utils();
